﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using WinformDevelopFrame.Entity;
using Template=NVelocity.Template;
namespace WinformDevelopFrame.Template
{
    public class WinformAdapter
    {
        private static readonly string templateDir = AppDomain.CurrentDomain.BaseDirectory + @"\Template\";
        public bool create(string tableName, string NameSpace, string outdir, List<TableInfo> EntityList,int num)
        {

            VelocityEngine velocityEngine = new VelocityEngine();
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, @"file");
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir);
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
            //模板的缓存设置
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);              //是否缓存
            props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30);    //缓存时间(秒)
            velocityEngine.Init(props);
            //为模板变量赋值
            VelocityContext context = new VelocityContext();
            context.Put("tableName", tableName);
            context.Put("NameSpace", NameSpace);
            context.Put("EntityList", EntityList);
            context.Put("ColumnNum", num);
            //从文件中读取模板
            NVelocity.Template template5 = velocityEngine.GetTemplate(@"\FormDesigner.vm");
            NVelocity.Template template6 = velocityEngine.GetTemplate(@"\Form.vm");
            if (!Directory.Exists(outdir + "\\UI"))
            {
                Directory.CreateDirectory(outdir + "\\UI");
            }
            using (StreamWriter writer = new StreamWriter(outdir + $"\\UI\\Frm{tableName}.Designer.cs", false))
            {
                template5.Merge(context, writer);
            }
            using (StreamWriter writer = new StreamWriter(outdir + $"\\UI\\Frm{tableName}.cs", false))
            {
                template6.Merge(context, writer);
            }
            return true;
        }
    }
}
